class Solution {
    int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1};
    int dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};
    int m, n;
 
public:
    vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) {
        m = board.size(), n = board[0].size();
        int x = click[0], y = click[1];
        if(board[x][y] == 'M')
        {
            board[x][y] = 'X';
            return board;
        }
        dfs(board, x, y);
        return board;
    }
 
    void dfs(vector<vector<char>>& board, int sr, int sc)
    {
        int cnt = 0;
        for(int i = 0; i < 8; ++i)
        {
            int x = sr + dx[i], y = sc + dy[i];
            if(x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'M')
                ++cnt;
        }
 
        if(cnt != 0)
        {
            board[sr][sc] = cnt + '0';
            return;
        }
        else // 周围没地雷
        {
            board[sr][sc] = 'B';
            for(int i = 0; i < 8; ++i)
            {
                int x = sr + dx[i], y = sc + dy[i];
                if(x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'E')
                    dfs(board, x, y); // 再探索周围的空白块
            }
        }
    }
};